package com.fengba.nanzhi.tabbar01;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.os.CountDownTimer;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.fengba.nanzhi.fengba.R;
import com.fengba.nanzhi.tabbar03.SubmitOrder;
import com.fengba.nanzhi.tabbar03.SubmitOrder_Dh;
import com.fengba.nanzhi.tabbar04.Tabbar04Item;
import com.fengba.nanzhi.utils.Function;
import com.fengba.nanzhi.utils.OKManager;
import com.fengba.nanzhi.utils.Preferences;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;
import java.util.List;

/**
 * Created by duan on 2018/4/12.
 */

public class ExchangeItemAdpter extends BaseAdapter{

    JSONArray dataArray;
    List<String> section;
    Context context;
    LayoutInflater inflater = null;
    //用于退出 Activity,避免 Countdown，造成资源浪费。
    private SparseArray<CountDownTimer> countDownCounters;


    public ExchangeItemAdpter(Context context, JSONArray dataArray){
        this.dataArray = dataArray;
        this.context = context;
        // 布局装载器对象
        this.inflater = LayoutInflater.from(context);
        this.countDownCounters = new SparseArray<>();

    }

    // 适配器中数据集中数据的个数
    @Override
    public int getCount() {
        try {
            return dataArray.length();
        }catch (Exception e){
            return 0;
        }
    }

    // 获取数据集中与指定索引对应的数据项
    //@Override
    public Object getItem(int position) {
        return null;
        //return list.get(position);
    }

    // 获取指定行对应的ID
    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View converView, ViewGroup parent){

        View view = null;
        //Tabbar04Item item = list.get(position);
        if(converView == null){
            // 当为null的时候，View未被实例化，缓存池中无缓存
            converView = inflater.inflate(R.layout.dmy_exchange_item, null);
            view = converView;
        }else{
            view = converView;
        }

        //商品图片
        final ImageView imageView = (ImageView)view.findViewById(R.id.item_good_img);
        //商品名称
        TextView textView = (TextView) view.findViewById(R.id.item_good_name);
        //商品价格
        TextView item_good_price = (TextView) view.findViewById(R.id.item_good_price);
        //商品初始价格
        TextView item_yuan_good_price1 = (TextView) view.findViewById(R.id.item_yuan_good_price1);
        //btn
        Button go2Submit = view.findViewById(R.id.go2Submit);

        try {
            //data
            final JSONObject  data = (JSONObject) dataArray.getJSONObject(position);
            //获取good数据
            JSONObject good = (JSONObject) dataArray.getJSONObject(position).get("goods");
            //获取good的图片
            String firstPic = good.get("images").toString().split(",")[0];
            //图片下载
            OKManager.getInstance().asyncBitmapByUrl(firstPic, new OKManager.func3() {
                @Override
                public void onResponse(Bitmap bitmap) {
                    imageView.setImageBitmap(bitmap);
                }
            });
            //图片名称
            textView.setText(dataArray.getJSONObject(position).get("goods_name").toString());
            //商品价格
            item_good_price.setText(dataArray.getJSONObject(position).get("credit").toString());
            //商品初始价格 && 中划线
            item_yuan_good_price1.setText("限量"+dataArray.getJSONObject(position).get("left_number").toString()+"件");
            //go
            go2Submit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, SubmitOrder_Dh.class);
                    intent.putExtra("good", data.toString());
                    context.startActivity(intent);
                }
            });
        }catch (JSONException e){
            e.printStackTrace();
        }

        return view;
    }

    /**
     * 将毫秒转化为 分钟：秒 的格式
     *
     * @param millisecond 毫秒
     * @return
     */
    public String formatTime(long millisecond) {
        int hour;//小时
        int minute;//分钟
        int second;//秒数
        hour   = (int) ((millisecond / 1000) / 60 / 60 );
        minute = (int) ((millisecond / 1000) / 60 % 60);
        second = (int) ((millisecond / 1000) % 60);
        if (minute < 10) {
            if (second < 10) {
                return hour+":"+"0" + minute + ":" + "0" + second;
            } else {
                return hour+":"+"0" + minute + ":" + second;
            }
        }else {
            if (second < 10) {
                return hour+":"+minute + ":" + "0" + second;
            } else {
                return hour+":"+minute + ":" + second;
            }
        }
    }

}
